package com.mall.order.services;

import com.mall.commons.tool.exception.BizException;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.OrderDeleteService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.DeleteOrderRequest;
import com.mall.order.dto.DeleteOrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Connor
 * @date 2020/12/21
 * @Version 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = OrderDeleteService.class)
public class OrderDeleteServiceImpl implements OrderDeleteService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderShippingMapper orderShippingMapper;

    @Autowired
    private StockMapper stockMapper;

    @Override
    public DeleteOrderResponse deleteOrder(DeleteOrderRequest deleteOrderRequest) {
        DeleteOrderResponse deleteOrderResponse = new DeleteOrderResponse();

        try {
            deleteOrderRequest.requestCheck();
        } catch (ValidateException e) {
            e.printStackTrace();
            deleteOrderResponse.setCode(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
            return deleteOrderResponse;
        }
        String orderId = deleteOrderRequest.getOrderId();
        int deletedOrderEffect = orderMapper.deleteByPrimaryKey(orderId);

        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderId", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        for (OrderItem item : orderItems) {
            Stock newStock = new Stock();
            newStock.setItemId(item.getItemId());
            newStock.setStockCount(item.getNum().longValue());
            newStock.setLockCount(-item.getNum());
            stockMapper.updateStock(newStock);
        }
        int deleteOrderItemEffect = orderItemMapper.deleteByExample(example);
        int deleteOrderShippingEffect = orderShippingMapper.deleteByPrimaryKey(orderId);
        // TODO: 为何tkmybatis的方法删除成功后返回0？
        /*if (deletedOrderEffect != 0 && deleteOrderItemEffect != 0 && deleteOrderShippingEffect != 0) {
            deleteOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } else {
            deleteOrderResponse.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            deleteOrderResponse.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }*/
        deleteOrderResponse.setCode(OrderRetCode.SUCCESS.getCode());
        deleteOrderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return deleteOrderResponse;
    }
}
